package com.netposa.datawarehouse.aggregator.web.rest;

import com.alibaba.fastjson.JSONObject;
import com.netposa.appwebres.WebResCallback;
import com.netposa.appwebres.common.WebResCriteria;
import com.netposa.appwebres.common.WebResInfo;
import com.netposa.datawarehouse.aggregator.client.DeviceServiceFeign;
import com.netposa.datawarehouse.aggregator.service.AggregatorService;
import com.netposa.datawarehouse.aggregator.web.enums.DeviceAbilityEnum;
import com.netposa.datawarehouse.aggregator.web.ext.PvmContextExt;
import com.netposa.datawarehouse.aggregator.web.ext.PvmRole;
import com.netposa.datawarehouse.aggregator.web.model.DeviceTreeCondition;
import com.netposa.datawarehouse.ftr.DataSearchClient;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"ftr"})
public class FullTextRetrievalController {
    private static final Logger log = LoggerFactory.getLogger(FullTextRetrievalController.class);
    @Autowired
    private AggregatorService aggregatorService;
    @Value("${netposa.aggregator.es.switch}")
    private boolean needEs;
    @Autowired
    private ApplicationContext applicationContext;
    private Integer ROLE_TYPE = 1;
    private String CODE_TYPE = "1";
    @Autowired
    private DeviceServiceFeign deviceServiceFeign;
    @Autowired
    private PvmContextExt pvmContextExt;

    private List getDeviceIdByRoleId(HttpServletRequest request) {
        List deviceIdList = new ArrayList();

        try {
            List roleList = this.pvmContextExt.getRole(request);
            Iterator var4 = roleList.iterator();

            while (var4.hasNext()) {
                PvmRole pvmRole = (PvmRole) var4.next();
                if (pvmRole.getType() == this.ROLE_TYPE) {
                    log.info("权限标记" + pvmRole.getCode());
                    if (this.CODE_TYPE.equals(pvmRole.getCode())) {
                        DeviceTreeCondition deviceTreeCondition = new DeviceTreeCondition();
                        List<String> ablilists = new ArrayList<>();
                        ablilists.add(DeviceAbilityEnum.TRAFFIC.getStringValue());
                        ablilists.add(DeviceAbilityEnum.FACE.getStringValue());
                        ablilists.add(DeviceAbilityEnum.BODY.getStringValue());
                        deviceTreeCondition.setAbilities(ablilists);
                        WebResInfo data = this.deviceServiceFeign.staypoint(deviceTreeCondition);
                        if ("200".equals(data.getCode())) {
                            List<Map<String, String>> devices = (ArrayList) data.getData();
                            deviceIdList = (List<String>) devices.stream().map(item -> (String) item.get("id")).collect(Collectors.toList());
                        }
                        return deviceIdList;
                    }
                }
            }
        } catch (Exception var10) {
            log.error(var10.getMessage(), var10);
        }

        return (List) deviceIdList;
    }

    /**
     * @deprecated
     */
    @Deprecated
    @GetMapping({"/full_text"})
    public WebResInfo fullText(final String searchText) {
        return (new WebResCallback() {
            public void execute(WebResCriteria criteria, Object... params) {
                DataSearchClient searchClient = (DataSearchClient) FullTextRetrievalController.this.applicationContext.getBean(DataSearchClient.class);
                criteria.addSingleResult(FullTextRetrievalController.this.aggregatorService.fullTextRetrieval(searchText, searchClient));
            }
        }).sendRequest(new Object[0]);
    }

    @PostMapping({"/all"})
    public WebResInfo fullTextRetrieval(@RequestBody final JSONObject condition, HttpServletRequest request) {
        Object devices = condition.get("deviceIds");
        if (null == devices || StringUtils.isEmpty(devices.toString())) {
            List deviceIdList = this.getDeviceIdByRoleId(request);
            if (deviceIdList != null && deviceIdList.size() > 0) {
                log.error("设备数量：" + deviceIdList.size());
                String deviceIds = org.apache.commons.lang3.StringUtils.join(deviceIdList, ",");
                condition.put("deviceIds", deviceIds);
            }
        }

        return (new WebResCallback() {
            public void execute(WebResCriteria criteria, Object... params) {
                if (FullTextRetrievalController.this.needEs) {
                    DataSearchClient searchClient = (DataSearchClient) FullTextRetrievalController.this.applicationContext.getBean(DataSearchClient.class);
                    criteria.addSingleResult(FullTextRetrievalController.this.aggregatorService.fullTextRetrievalWithAll(condition, searchClient));
                } else {
                    criteria.addSingleResult(FullTextRetrievalController.this.aggregatorService.selectAllInModules(condition));
                }

            }
        }).sendRequest(new Object[0]);
    }
}
